<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 */
//用户端登陆
//基本思路：当需要用户登陆方可访问时，跳转至此页。先检测用户环境（微信端、手机端、pc端、小程序端）
//微信端和小程序端统一处理：如果存在$_SESSION['openid']则根据openid来登陆
//微信端如果没能获取到openid，则类比普通手机端处理
//普通手机端和pc端统一处理
//总结精炼：登陆（注册）前不判断，登陆（注册）后，如果session中有openid，则把当前粉丝与登陆的用户绑定
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return['account']=$_SESSION['account'];
$return['extradata']=array();
$setting=uni_setting('register');
$smsset=uni_setting('sms');
if($setting['dxmb'] && $smsset['plugin'] && $setting['login_type']){
	$return['issendcode']=1;
}elseif($return['pluginset']['is_mobile']){
	$return['issendcode']=1;
}
$_SESSION['isauth_base']=0;
if($_RQ['op']=='get'){
	$return['nosharecover']=$_RQ['nosharecover'];
	$return['item']=array();
	$return['title']='注册';
	$item=array();
	$item['show_type']=$setting['login_type']==2?true:false;
	$item['login_type']=$setting['login_type'];
	$return['setting']=$setting;
	$item['xieyi_title']=$setting['xieyi_title'];
	$item['user_xieyi']=$setting['user_xieyi'];
	if($_RQ['fromuser'] || $_RQ['snmid']){
		$item['isfromuser']=1;
		$item['fromcode']=$_RQ['snmid']?:$_RQ['fromuser'];
	}
	$app=pdo_get('jy_saleapp_list',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	$app['saleset']=json_decode($app['saleset'],true);
	$return['app']=$app;
	$item['appid']=$app['id'];
	$return['item']=$item;
	$return['installbak']=tomedia($app['installbak']);
	$return['installbak2']=tomedia($app['installbak2']);
	exi($return);
}
if($_RQ['op']=='sendcode'){
	$post=get_postdata();
	$mobile=$post['mobile'];
	$code=rand(100000,999999);
	$verify=array(
		'mobile'=>$mobile,
		'code'=>$code,
	);
	buffer_save($verify,'core_sms_verify');
	$content=array('code'=>$code);
	$template_id=$setting['dxmb'];
	if(!$template_id){
		exi('管理员未设置系统短信验证码模版，请联系管理员设置(站点设置--站点注册设置--验证码短信模版ID)！','error');
	}
	$result=cfc('core')->sendsms($mobile,$content,$template_id);
	if(is_error($result)){
		exi('验证码发送失败：'.$result['message'],'error');
	}
	if(!$result){
		exi('error');
	}else{
		exi('success');
	}
}
if($_RQ['op']=='register'){
	//注册
	$post=get_postdata();
	if($_SESSION['uid']){
		//exi('您已登录！无需再注册！','error');
	}
	if(!$post['mobile']){
		exi('请输入账号或手机号！','error');
	}
	if($return['issendcode']==1){
		//手机验证码注册
		if(!check_sms_code($post['smscode'])){
			exi('手机验证码错误！','error');
		}
		if($setting['more_mobile']){
			if($post['username'] == $post['password']){
				exi('账号跟密码不能相同！','error');
			}
			if(!$post['password'] || !$post['password2']){
				exi('请输入密码！','error');
			}
			if($post['password'] != $post['password2']){
				exi('两次输入密码不一致！','error');
			}
		}else{
			$post['username']=$post['mobile'];
		}
	}else{
		$post['username']=$post['mobile'];
		if($post['mobile'] == $post['password']){
			exi('账号跟密码不能相同！','error');
		}
		if(!$post['password'] || !$post['password2']){
			exi('请输入密码！','error');
		}
		if($post['password'] != $post['password2']){
			exi('两次输入密码不一致！','error');
		}
	}
	$uid=pdo_getcolumn('core_members',array('username'=>trim($post['username']),'uniacid'=>$_SESSION['uniacid']),'id');
	if($uid){
		exi('账号已存在，请更换!','error');
	}
	$member=array('username'=>trim($post['username']),'mobile'=>trim($post['mobile']));
	if($return['issendcode']==1){
		$member['realmobile']=trim($post['mobile']);
		if($setting['more_mobile']){
			$member['password']=trim($post['password']);
		}
	}else{
		$member['password']=trim($post['password']);
	}
	$fromcode=pdo_getcolumn('core_members',array('snmid'=>trim($post['fromcode']),'uniacid'=>$_SESSION['uniacid']),'id');
	if(!$fromcode){
		$fromcode=pdo_getcolumn('core_members',array('id'=>trim($post['fromcode']),'uniacid'=>$_SESSION['uniacid']),'id');
		if(!$fromcode && $post['fromcode']){
			exi('邀请码不存在，不能注册!','error');
		}
	}
	$member['fromuser']=$member['fromcode']=$fromcode;
	if($post['check']!=1 && $post['needcheck']){
		exi('请先阅读协议！','error');
	}
	$uid=model('core/member')->register($member,true,$setting['must_check']);
	$member['id']=$uid;
	$this->model('member')->upfeelog($member,$post['appid']);
	if($setting['must_check']==1){
		exi('等待审核！','error');
	}
	exi('success');
}
if($_RQ['op']=='login'){
	//登陆
	$post=get_postdata();
	if(!$post['mobile']){
		exi('请输入账户名或手机号！','error');
	}
	if($return['issendcode']==1){
		//手机验证码登陆
		if(!check_sms_code($post['smscode'])){
			exi('手机验证码错误！','error');
		}
	}
	$member=pdo_get('core_members',array('username'=>trim($post['mobile']),'uniacid'=>$_SESSION['uniacid']),array('id','salt','password','onemid','nocheck','fromuser'));
	if(!$member['id']){
		$member=pdo_get('core_members',array('mobile'=>trim($post['mobile']),'uniacid'=>$_SESSION['uniacid']),array('id','salt','password','onemid','nocheck','fromuser'));
	}
	$uid=$member['onemid']?:$member['id'];
	if(!$uid){
		exi('用户不存在，请先注册!'.$post['mobile'],'error');
	}
	if($return['issendcode']!=1){
		//账号密码登陆
		if(!$post['password']){
			exi('请输入密码！','error');
		}
		if(!check_password($post['password'],$member['salt'],$member['password'])){
			exi('账号或密码错误!','error');
		}
	}
	if($member['nocheck']==1){
		exi('该用户属于待审核状态!不能登录！','error');
	}
	if($post['fromcode'] && !$member['fromuser']){
		$fromcode=pdo_getcolumn('core_members',array('snmid'=>trim($post['fromcode']),'uniacid'=>$_SESSION['uniacid']),'id');
		if(!$fromcode){
			$fromcode=pdo_getcolumn('core_members',array('id'=>trim($post['fromcode']),'uniacid'=>$_SESSION['uniacid']),'id');
		}
		pdo_update('core_members',array('fromuser'=>$fromcode),array('id'=>$uid));
		$member['fromuser']=$fromcode;
		$this->model('member')->upfeelog($member,$post['appid']);
	}
	exi('success');
}
?>